<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Contents &mdash; ib_insync 0.9.86 documentation</title>
      <link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b76e3c8a" />
      <link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=19f00094" />

  
    <link rel="canonical" href="https://ib_insync.readthedocs.ioindex.html"/>
  <!--[if lt IE 9]>
    <script src="_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="_static/jquery.js?v=5d32c60e"></script>
        <script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="_static/documentation_options.js?v=b76974d2"></script>
        <script src="_static/doctools.js?v=888ff710"></script>
        <script src="_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Introduction" href="readme.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="#" class="icon icon-home">
            ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="#">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="#" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">Contents</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="contents">
<h1>Contents<a class="headerlink" href="#contents" title="Link to this heading"></a></h1>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="readme.html">Introduction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="readme.html#installation">Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="readme.html#example">Example</a></li>
<li class="toctree-l2"><a class="reference internal" href="readme.html#documentation">Documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="readme.html#discussion">Discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="readme.html#disclaimer">Disclaimer</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API docs</a><ul>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.ib">IB</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.client">Client</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.order">Order</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.contract">Contract</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.ticker">Ticker</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.objects">Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.util">Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#module-ib_insync.flexreport">FlexReport</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#ibc">IBC</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#watchdog">Watchdog</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="notebooks.html">Notebooks</a><ul>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/basics.ipynb">Basics</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/contract_details.ipynb">Contract details</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/option_chain.ipynb">Option chain</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/bar_data.ipynb">Bar data</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/tick_data.ipynb">Tick data</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/market_depth.ipynb">Market depth</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/ordering.ipynb">Ordering</a></li>
<li class="toctree-l2"><a class="reference external" href="https://nbviewer.jupyter.org/github/erdewit/ib_insync/tree/master/notebooks/scanners.ipynb">Scanners</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="recipes.html">Code recipes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#fetching-consecutive-historical-data">Fetching consecutive historical data</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#scanner-data-blocking">Scanner data (blocking)</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#scanner-data-streaming">Scanner data (streaming)</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#option-calculations">Option calculations</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#order-book">Order book</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#minimum-price-increments">Minimum price increments</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#news-articles">News articles</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#news-bulletins">News bulletins</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#wsh-event-calendar">WSH Event Calendar</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#dividends">Dividends</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#fundemental-ratios">Fundemental ratios</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#short-lived-connections">Short-lived connections</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#integration-with-pyqt5-or-pyside2">Integration with PyQt5 or PySide2</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#integration-with-tkinter">Integration with Tkinter</a></li>
<li class="toctree-l2"><a class="reference internal" href="recipes.html#integration-with-pygame">Integration with PyGame</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Source code</a><ul>
<li class="toctree-l2"><a class="reference external" href="https://github.com/erdewit/ib_insync">Download</a></li>
<li class="toctree-l2"><a class="reference external" href="https://github.com/erdewit/ib_insync/issues">Issue Tracker</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a><ul>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#id1">0.9</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#id2">0.8</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#id3">0.7</a></li>
<li class="toctree-l2"><a class="reference internal" href="changelog.html#id4">0.6</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="links.html">Links</a></li>
</ul>
</div>
<p><a class="reference external" href="https://github.com/erdewit/ib_insync/actions"><img alt="Build" src="https://github.com/erdewit/ib_insync/actions/workflows/test.yml/badge.svg?branch=master" /></a> <a class="reference external" href="https://groups.io/g/insync"><img alt="Join the user group" src="https://img.shields.io/badge/groups.io-insync-green.svg" /></a> <img alt="" src="https://img.shields.io/badge/python-3.6+-blue.svg" /> <img alt="" src="https://img.shields.io/badge/status-beta-green.svg" /> <a class="reference external" href="https://pypi.python.org/pypi/ib_insync"><img alt="PyPi" src="https://img.shields.io/pypi/v/ib_insync.svg" /></a> <img alt="" src="https://img.shields.io/badge/license-BSD-blue.svg" /> <a class="reference external" href="https://pepy.tech/project/ib-insync"><img alt="Number of downloads" src="https://static.pepy.tech/badge/ib-insync" /></a> <a class="reference external" href="https://ib-insync.readthedocs.io/api.html"><img alt="Documentation" src="https://img.shields.io/badge/Documentation-green.svg" /></a></p>
</section>
<section id="introduction">
<h1>Introduction<a class="headerlink" href="#introduction" title="Link to this heading"></a></h1>
<p>The goal of the IB-insync library is to make working with the
<a class="reference external" href="http://interactivebrokers.github.io/tws-api/">Trader Workstation API</a>
from Interactive Brokers as easy as possible.</p>
<p>The main features are:</p>
<ul class="simple">
<li><p>An easy to use linear style of programming;</p></li>
<li><p>An <a class="reference external" href="https://ib-insync.readthedocs.io/api.html#module-ib_insync.ib">IB component</a>
that automatically keeps in sync with the TWS or IB Gateway application;</p></li>
<li><p>A fully asynchonous framework based on
<a class="reference external" href="https://docs.python.org/3/library/asyncio.html">asyncio</a>
and
<a class="reference external" href="https://github.com/erdewit/eventkit">eventkit</a>
for advanced users;</p></li>
<li><p>Interactive operation with live data in Jupyter notebooks.</p></li>
</ul>
<p>Be sure to take a look at the
<a class="reference external" href="https://ib-insync.readthedocs.io/notebooks.html">notebooks</a>,
the <a class="reference external" href="https://ib-insync.readthedocs.io/recipes.html">recipes</a>
and the <a class="reference external" href="https://ib-insync.readthedocs.io/api.html">API docs</a>.</p>
<section id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Link to this heading"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">ib_insync</span>
</pre></div>
</div>
<p>Requirements:</p>
<ul class="simple">
<li><p>Python 3.6 or higher;</p></li>
<li><p>A running TWS or IB Gateway application (version 1023 or higher).
Make sure the
<a class="reference external" href="https://interactivebrokers.github.io/tws-api/initial_setup.html">API port is enabled</a>
and ‘Download open orders on connection’ is checked.</p></li>
</ul>
<p>The ibapi package from IB is not needed.</p>
</section>
<section id="example">
<h2>Example<a class="headerlink" href="#example" title="Link to this heading"></a></h2>
<p>This is a complete script to download historical data:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">ib_insync</span> <span class="kn">import</span> <span class="o">*</span>
<span class="c1"># util.startLoop()  # uncomment this line when in a notebook</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">contract</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;EURUSD&#39;</span><span class="p">)</span>
<span class="n">bars</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
    <span class="n">contract</span><span class="p">,</span> <span class="n">endDateTime</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">durationStr</span><span class="o">=</span><span class="s1">&#39;30 D&#39;</span><span class="p">,</span>
    <span class="n">barSizeSetting</span><span class="o">=</span><span class="s1">&#39;1 hour&#39;</span><span class="p">,</span> <span class="n">whatToShow</span><span class="o">=</span><span class="s1">&#39;MIDPOINT&#39;</span><span class="p">,</span> <span class="n">useRTH</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="c1"># convert to pandas dataframe (pandas needs to be installed):</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">df</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>                   <span class="n">date</span>      <span class="nb">open</span>      <span class="n">high</span>       <span class="n">low</span>     <span class="n">close</span>  <span class="n">volume</span>  \
<span class="mi">0</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">19</span> <span class="mi">23</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107875</span>  <span class="mf">1.108050</span>  <span class="mf">1.107725</span>  <span class="mf">1.107825</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">1</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107825</span>  <span class="mf">1.107925</span>  <span class="mf">1.107675</span>  <span class="mf">1.107825</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">2</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">01</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107825</span>  <span class="mf">1.107975</span>  <span class="mf">1.107675</span>  <span class="mf">1.107875</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">3</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">02</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107875</span>  <span class="mf">1.107975</span>  <span class="mf">1.107025</span>  <span class="mf">1.107225</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">4</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">03</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107225</span>  <span class="mf">1.107725</span>  <span class="mf">1.107025</span>  <span class="mf">1.107525</span>      <span class="o">-</span><span class="mi">1</span>
<span class="o">..</span>                  <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>     <span class="o">...</span>
<span class="mi">705</span> <span class="mi">2020</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">14</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.119325</span>  <span class="mf">1.119675</span>  <span class="mf">1.119075</span>  <span class="mf">1.119225</span>      <span class="o">-</span><span class="mi">1</span>
</pre></div>
</div>
</section>
<section id="documentation">
<h2>Documentation<a class="headerlink" href="#documentation" title="Link to this heading"></a></h2>
<p>The complete <a class="reference external" href="https://ib-insync.readthedocs.io/api.html">API documentation</a>.</p>
<p><a class="reference external" href="https://ib-insync.readthedocs.io/changelog.html">Changelog</a>.</p>
</section>
<section id="discussion">
<h2>Discussion<a class="headerlink" href="#discussion" title="Link to this heading"></a></h2>
<p>The <a class="reference external" href="https://groups.io/g/insync">insync user group</a> is the place to discuss
IB-insync and anything related to it.</p>
</section>
<section id="disclaimer">
<h2>Disclaimer<a class="headerlink" href="#disclaimer" title="Link to this heading"></a></h2>
<p>The software is provided on the conditions of the simplified BSD license.</p>
<p>This project is not affiliated with Interactive Brokers Group, Inc.’s.</p>
<p>Good luck and enjoy,</p>
<dl class="field-list simple">
<dt class="field-odd">author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Ewald de Wit &lt;<a class="reference external" href="mailto:ewald&#46;de&#46;wit&#37;&#52;&#48;gmail&#46;com">ewald<span>&#46;</span>de<span>&#46;</span>wit<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</p>
</dd>
</dl>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="readme.html" class="btn btn-neutral float-right" title="Introduction" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2023, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>